Cherry Swap Backtesting

In [1]:
import chart_studio.plotly as py
import plotly.graph_objs as go
import pandas as pd
import numpy as np
import requests
import ssl
import time
import math
import plotly.express as px
In [2]:
#disable ssl for cryptory API & virtualenv
ssl._create_default_https_context = ssl._create_unverified_context

Request Data from Compound.finance API

In [3]:
cDaiAddress = '0xf5dce57282a584d2746faf1593d3121fcac444dc'
endTimestamp = math.floor(time.time())
startTimeStamp = endTimestamp - 6 * 30 * 24 * 60 * 60 #use the most recent 6 month's worth of data.
num_buckets = 24 * 30
requestURL = "https://api.compound.finance/api/v2/market_history/graph?asset=%s&min_block_timestamp=%s&max_block_timestamp=%s&num_buckets=%s"% (cDaiAddress, startTimeStamp, endTimestamp, num_buckets)

response = requests.get(requestURL)

Convert Data into dataframe

In [4]:
interestOverTime = pd.DataFrame.from_dict(response.json()['borrow_rates'])
interestOverTime.head()
interestOverTime['block_time'] = pd.to_datetime(interestOverTime['block_timestamp'],unit='s')
interestOverTime['rate_per_block'] = interestOverTime['rate'] / (4 * 60 * 24 * 365)
interestOverTime.head(20)
Out[4]:
block_number block_timestamp rate block_time rate_per_block
0 7710279 1555742536 0.050000 2019-04-20 06:42:16 2.378234e-08
1 7711719 1555764136 0.050000 2019-04-20 12:42:16 2.378234e-08
2 7714599 1555807336 0.052989 2019-04-21 00:42:16 2.520426e-08
3 7716039 1555828936 0.055972 2019-04-21 06:42:16 2.662271e-08
4 7717479 1555850536 0.055955 2019-04-21 12:42:16 2.661480e-08
5 7718919 1555872136 0.055978 2019-04-21 18:42:16 2.662554e-08
6 7720359 1555893736 0.084921 2019-04-22 00:42:16 4.039225e-08
7 7721799 1555915336 0.084682 2019-04-22 06:42:16 4.027894e-08
8 7723239 1555936936 0.080160 2019-04-22 12:42:16 3.812794e-08
9 7727559 1556001736 0.084223 2019-04-23 06:42:16 4.006063e-08
10 7728999 1556023336 0.091294 2019-04-23 12:42:16 4.342372e-08
11 7731879 1556066536 0.090443 2019-04-24 00:42:16 4.301890e-08
12 7733319 1556088136 0.090478 2019-04-24 06:42:16 4.303545e-08
13 7737639 1556152936 0.090464 2019-04-25 00:42:16 4.302896e-08
14 7752039 1556368936 0.090430 2019-04-27 12:42:16 4.301257e-08
15 7753479 1556390536 0.087537 2019-04-27 18:42:16 4.163694e-08
16 7754919 1556412136 0.090387 2019-04-28 00:42:16 4.299240e-08
17 7757799 1556455336 0.091951 2019-04-28 12:42:16 4.373602e-08
18 7759239 1556476936 0.092527 2019-04-28 18:42:16 4.401031e-08
19 7763559 1556541736 0.093096 2019-04-29 12:42:16 4.428091e-08

Plot Intrest rate over time

In [5]:
data = [go.Scatter(
        x=interestOverTime['block_time'],
        y=interestOverTime['rate']
    )]

layout = go.Layout(
    title='Compound Interst Rate Over time',
    yaxis=dict(title='Lending Rate (%)'),
    xaxis=dict(title='Date'),
    template='plotly_white')

figure = go.Figure(data=data, layout=layout)

figure.show()

Generate pools

In [6]:
pool = pd.DataFrame()
pool['L'] = 2 * list(range(100, 1101, 100))
pool['S'] = 2 * list(range(1100, 0, -100))
pool['Total'] = pool.L + pool.S
pool['is_long'] = 11 * [1] + 11 * [0]
pool['percentage'] = pool.is_long * pool.L / pool.Total + (1 - pool.is_long) * pool.S / pool.Total
pool
Out[6]:
L S Total is_long percentage
0 100 1100 1200 1 0.083333
1 200 1000 1200 1 0.166667
2 300 900 1200 1 0.250000
3 400 800 1200 1 0.333333
4 500 700 1200 1 0.416667
5 600 600 1200 1 0.500000
6 700 500 1200 1 0.583333
7 800 400 1200 1 0.666667
8 900 300 1200 1 0.750000
9 1000 200 1200 1 0.833333
10 1100 100 1200 1 0.916667
11 100 1100 1200 0 0.916667
12 200 1000 1200 0 0.833333
13 300 900 1200 0 0.750000
14 400 800 1200 0 0.666667
15 500 700 1200 0 0.583333
16 600 600 1200 0 0.500000
17 700 500 1200 0 0.416667
18 800 400 1200 0 0.333333
19 900 300 1200 0 0.250000
20 1000 200 1200 0 0.166667
21 1100 100 1200 0 0.083333

The Model

  • pools: $P_L^0 + P_S^0 = P_T^0$
  • short pool at $t+1$: $P_S^{1, fix} = P_S^0 (1 + i_0)^n$ where $i_0$ is the interest per block at $t=0$ and $n$ is the number of blocks mined between $t=1$ and $t=0$
  • total pool at $t+1$: $P_T^{1, float} = P_T^0 \Pi_k (1 + i_k)^{n_k}$
  • payout short: $p_j^S = f_j^S P_S^{1, fix}$ where $f_j^S$ is the fraction participant $j$ holds in the short pool
  • payout long: $p_j^L = f_j^L (P_T^{1, float} - P_S^{1, fix})$
In [7]:
bt1 = pool.copy()
bt1['Total_float'] = pool.Total
i = 1
while interestOverTime['block_timestamp'][i] < interestOverTime['block_timestamp'][0] + 30 * 24 * 60 * 60:
    number_of_blocks = (interestOverTime['block_timestamp'][i] - interestOverTime['block_timestamp'][i-1]) / 15
    bt1['Total_float'] = bt1['Total_float'] * (1 + interestOverTime['rate_per_block'][i-1])**number_of_blocks
    i += 1
bt1['Total_fixed'] = bt1.Total * (1 + interestOverTime['rate_per_block'][0])**((interestOverTime['block_timestamp'][i] - interestOverTime['block_timestamp'][0]) / 15)
bt1['payout'] = bt1.is_long * (bt1.Total_float - (bt1.S/bt1.Total) * bt1.Total_fixed) + (1 - bt1.is_long) * (bt1.S/bt1.Total) * bt1.Total_fixed
bt1['non_pool_float_payout'] = bt1.percentage * bt1.Total_float
bt1['profit'] = bt1.payout - bt1.non_pool_float_payout
bt1
Out[7]:
L S Total is_long percentage Total_float Total_fixed payout non_pool_float_payout profit
0 100 1100 1200 1 0.083333 1210.999694 1204.941654 106.469844 100.916641 5.553203
1 200 1000 1200 1 0.166667 1210.999694 1204.941654 206.881649 201.833282 5.048367
2 300 900 1200 1 0.250000 1210.999694 1204.941654 307.293453 302.749923 4.543530
3 400 800 1200 1 0.333333 1210.999694 1204.941654 407.705258 403.666565 4.038693
4 500 700 1200 1 0.416667 1210.999694 1204.941654 508.117062 504.583206 3.533857
5 600 600 1200 1 0.500000 1210.999694 1204.941654 608.528867 605.499847 3.029020
6 700 500 1200 1 0.583333 1210.999694 1204.941654 708.940671 706.416488 2.524183
7 800 400 1200 1 0.666667 1210.999694 1204.941654 809.352476 807.333129 2.019347
8 900 300 1200 1 0.750000 1210.999694 1204.941654 909.764280 908.249770 1.514510
9 1000 200 1200 1 0.833333 1210.999694 1204.941654 1010.176085 1009.166411 1.009673
10 1100 100 1200 1 0.916667 1210.999694 1204.941654 1110.587889 1110.083053 0.504837
11 100 1100 1200 0 0.916667 1210.999694 1204.941654 1104.529849 1110.083053 -5.553203
12 200 1000 1200 0 0.833333 1210.999694 1204.941654 1004.118045 1009.166411 -5.048367
13 300 900 1200 0 0.750000 1210.999694 1204.941654 903.706240 908.249770 -4.543530
14 400 800 1200 0 0.666667 1210.999694 1204.941654 803.294436 807.333129 -4.038693
15 500 700 1200 0 0.583333 1210.999694 1204.941654 702.882631 706.416488 -3.533857
16 600 600 1200 0 0.500000 1210.999694 1204.941654 602.470827 605.499847 -3.029020
17 700 500 1200 0 0.416667 1210.999694 1204.941654 502.059022 504.583206 -2.524183
18 800 400 1200 0 0.333333 1210.999694 1204.941654 401.647218 403.666565 -2.019347
19 900 300 1200 0 0.250000 1210.999694 1204.941654 301.235413 302.749923 -1.514510
20 1000 200 1200 0 0.166667 1210.999694 1204.941654 200.823609 201.833282 -1.009673
21 1100 100 1200 0 0.083333 1210.999694 1204.941654 100.411804 100.916641 -0.504837

Starting from the begining of the data set we generate a number of pools that have a month in duration. Each j represents this pool.

In [8]:
backtest = pool.copy()
j = 0
while interestOverTime['block_timestamp'][j] + 30 * 24 * 60 * 60 <= interestOverTime['block_timestamp'].iloc[-1]:
    bt = pool.copy()
    bt['Total_float'] = pool.Total
    i = 1
    while interestOverTime['block_timestamp'][j+i] < interestOverTime['block_timestamp'][j] + 30 * 24 * 60 * 60:
        number_of_blocks = (interestOverTime['block_timestamp'][j+i] - interestOverTime['block_timestamp'][j+i-1]) / 15
        bt['Total_float'] = bt['Total_float'] * (1 + interestOverTime['rate_per_block'][j+i-1])**number_of_blocks
        i += 1
    bt['Total_fixed'] = bt.Total * (1 + interestOverTime['rate_per_block'][j])**((interestOverTime['block_timestamp'][j+i] - interestOverTime['block_timestamp'][j]) / 15)
    bt['payout'] = bt.is_long * (bt.Total_float - (bt.S/bt.Total) * bt.Total_fixed) + (1 - bt.is_long) * (bt.S/bt.Total) * bt.Total_fixed
    bt['non_pool_float_payout'] = bt.percentage * bt.Total_float
    bt['profit'] = bt.payout - bt.non_pool_float_payout
    backtest[j] = bt.profit
    j += 1
In [9]:
backtest
Out[9]:
L S Total is_long percentage 0 1 2 3 4 ... 496 497 498 499 500 501 502 503 504 505
0 100 1100 1200 1 0.083333 5.553203 5.623012 5.489911 5.286191 5.349662 ... -1.628582 -1.606709 -1.603597 -1.617874 -1.637517 -1.635322 -1.663925 -1.635475 -1.671526 -1.631945
1 200 1000 1200 1 0.166667 5.048367 5.111830 4.990828 4.805628 4.863329 ... -1.480529 -1.460645 -1.457815 -1.470795 -1.488652 -1.486656 -1.512659 -1.486796 -1.519569 -1.483586
2 300 900 1200 1 0.250000 4.543530 4.600647 4.491746 4.325065 4.376996 ... -1.332476 -1.314580 -1.312034 -1.323715 -1.339787 -1.337991 -1.361393 -1.338116 -1.367612 -1.335227
3 400 800 1200 1 0.333333 4.038693 4.089464 3.992663 3.844502 3.890663 ... -1.184423 -1.168516 -1.166252 -1.176636 -1.190921 -1.189325 -1.210127 -1.189436 -1.215655 -1.186869
4 500 700 1200 1 0.416667 3.533857 3.578281 3.493580 3.363940 3.404331 ... -1.036370 -1.022451 -1.020471 -1.029556 -1.042056 -1.040659 -1.058861 -1.040757 -1.063698 -1.038510
5 600 600 1200 1 0.500000 3.029020 3.067098 2.994497 2.883377 2.917998 ... -0.888317 -0.876387 -0.874689 -0.882477 -0.893191 -0.891994 -0.907595 -0.892077 -0.911741 -0.890152
6 700 500 1200 1 0.583333 2.524183 2.555915 2.495414 2.402814 2.431665 ... -0.740264 -0.730322 -0.728908 -0.735397 -0.744326 -0.743328 -0.756329 -0.743398 -0.759785 -0.741793
7 800 400 1200 1 0.666667 2.019347 2.044732 1.996331 1.922251 1.945332 ... -0.592211 -0.584258 -0.583126 -0.588318 -0.595461 -0.594663 -0.605064 -0.594718 -0.607828 -0.593434
8 900 300 1200 1 0.750000 1.514510 1.533549 1.497249 1.441688 1.458999 ... -0.444159 -0.438193 -0.437345 -0.441238 -0.446596 -0.445997 -0.453798 -0.446039 -0.455871 -0.445076
9 1000 200 1200 1 0.833333 1.009673 1.022366 0.998166 0.961126 0.972666 ... -0.296106 -0.292129 -0.291563 -0.294159 -0.297730 -0.297331 -0.302532 -0.297359 -0.303914 -0.296717
10 1100 100 1200 1 0.916667 0.504837 0.511183 0.499083 0.480563 0.486333 ... -0.148053 -0.146064 -0.145782 -0.147079 -0.148865 -0.148666 -0.151266 -0.148680 -0.151957 -0.148359
11 100 1100 1200 0 0.916667 -5.553203 -5.623012 -5.489911 -5.286191 -5.349662 ... 1.628582 1.606709 1.603597 1.617874 1.637517 1.635322 1.663925 1.635475 1.671526 1.631945
12 200 1000 1200 0 0.833333 -5.048367 -5.111830 -4.990828 -4.805628 -4.863329 ... 1.480529 1.460645 1.457815 1.470795 1.488652 1.486656 1.512659 1.486796 1.519569 1.483586
13 300 900 1200 0 0.750000 -4.543530 -4.600647 -4.491746 -4.325065 -4.376996 ... 1.332476 1.314580 1.312034 1.323715 1.339787 1.337991 1.361393 1.338116 1.367612 1.335227
14 400 800 1200 0 0.666667 -4.038693 -4.089464 -3.992663 -3.844502 -3.890663 ... 1.184423 1.168516 1.166252 1.176636 1.190921 1.189325 1.210127 1.189436 1.215655 1.186869
15 500 700 1200 0 0.583333 -3.533857 -3.578281 -3.493580 -3.363940 -3.404331 ... 1.036370 1.022451 1.020471 1.029556 1.042056 1.040659 1.058861 1.040757 1.063698 1.038510
16 600 600 1200 0 0.500000 -3.029020 -3.067098 -2.994497 -2.883377 -2.917998 ... 0.888317 0.876387 0.874689 0.882477 0.893191 0.891994 0.907595 0.892077 0.911741 0.890152
17 700 500 1200 0 0.416667 -2.524183 -2.555915 -2.495414 -2.402814 -2.431665 ... 0.740264 0.730322 0.728908 0.735397 0.744326 0.743328 0.756329 0.743398 0.759785 0.741793
18 800 400 1200 0 0.333333 -2.019347 -2.044732 -1.996331 -1.922251 -1.945332 ... 0.592211 0.584258 0.583126 0.588318 0.595461 0.594663 0.605064 0.594718 0.607828 0.593434
19 900 300 1200 0 0.250000 -1.514510 -1.533549 -1.497249 -1.441688 -1.458999 ... 0.444159 0.438193 0.437345 0.441238 0.446596 0.445997 0.453798 0.446039 0.455871 0.445076
20 1000 200 1200 0 0.166667 -1.009673 -1.022366 -0.998166 -0.961126 -0.972666 ... 0.296106 0.292129 0.291563 0.294159 0.297730 0.297331 0.302532 0.297359 0.303914 0.296717
21 1100 100 1200 0 0.083333 -0.504837 -0.511183 -0.499083 -0.480563 -0.486333 ... 0.148053 0.146064 0.145782 0.147079 0.148865 0.148666 0.151266 0.148680 0.151957 0.148359

22 rows × 511 columns

In [10]:
backtest_transpose = backtest.iloc[:,7:].transpose()
backtest_transpose['start_time'] = interestOverTime.iloc[:len(backtest_transpose.iloc[:, 0]), 3]
In [11]:
backtest_transpose
Out[11]:
0 1 2 3 4 5 6 7 8 9 ... 13 14 15 16 17 18 19 20 21 start_time
2 5.489911 4.990828 4.491746 3.992663 3.493580 2.994497 2.495414 1.996331 1.497249 0.998166 ... -4.491746 -3.992663 -3.493580 -2.994497 -2.495414 -1.996331 -1.497249 -0.998166 -0.499083 2019-04-21 00:42:16
3 5.286191 4.805628 4.325065 3.844502 3.363940 2.883377 2.402814 1.922251 1.441688 0.961126 ... -4.325065 -3.844502 -3.363940 -2.883377 -2.402814 -1.922251 -1.441688 -0.961126 -0.480563 2019-04-21 06:42:16
4 5.349662 4.863329 4.376996 3.890663 3.404331 2.917998 2.431665 1.945332 1.458999 0.972666 ... -4.376996 -3.890663 -3.404331 -2.917998 -2.431665 -1.945332 -1.458999 -0.972666 -0.486333 2019-04-21 12:42:16
5 5.406819 4.915290 4.423761 3.932232 3.440703 2.949174 2.457645 1.966116 1.474587 0.983058 ... -4.423761 -3.932232 -3.440703 -2.949174 -2.457645 -1.966116 -1.474587 -0.983058 -0.491529 2019-04-21 18:42:16
6 2.834139 2.576490 2.318841 2.061192 1.803543 1.545894 1.288245 1.030596 0.772947 0.515298 ... -2.318841 -2.061192 -1.803543 -1.545894 -1.288245 -1.030596 -0.772947 -0.515298 -0.257649 2019-04-22 00:42:16
7 2.892361 2.629419 2.366477 2.103535 1.840593 1.577651 1.314709 1.051768 0.788826 0.525884 ... -2.366477 -2.103535 -1.840593 -1.577651 -1.314709 -1.051768 -0.788826 -0.525884 -0.262942 2019-04-22 06:42:16
8 3.340473 3.036793 2.733114 2.429435 2.125755 1.822076 1.518397 1.214717 0.911038 0.607359 ... -2.733114 -2.429435 -2.125755 -1.822076 -1.518397 -1.214717 -0.911038 -0.607359 -0.303679 2019-04-22 12:42:16
9 3.096465 2.814968 2.533472 2.251975 1.970478 1.688981 1.407484 1.125987 0.844491 0.562994 ... -2.533472 -2.251975 -1.970478 -1.688981 -1.407484 -1.125987 -0.844491 -0.562994 -0.281497 2019-04-23 06:42:16
10 2.490687 2.264261 2.037835 1.811409 1.584982 1.358556 1.132130 0.905704 0.679278 0.452852 ... -2.037835 -1.811409 -1.584982 -1.358556 -1.132130 -0.905704 -0.679278 -0.452852 -0.226426 2019-04-23 12:42:16
11 2.630581 2.391437 2.152293 1.913150 1.674006 1.434862 1.195718 0.956575 0.717431 0.478287 ... -2.152293 -1.913150 -1.674006 -1.434862 -1.195718 -0.956575 -0.717431 -0.478287 -0.239144 2019-04-24 00:42:16
12 2.657373 2.415794 2.174214 1.932635 1.691056 1.449476 1.207897 0.966318 0.724738 0.483159 ... -2.174214 -1.932635 -1.691056 -1.449476 -1.207897 -0.966318 -0.724738 -0.483159 -0.241579 2019-04-24 06:42:16
13 2.738664 2.489695 2.240725 1.991756 1.742786 1.493817 1.244847 0.995878 0.746908 0.497939 ... -2.240725 -1.991756 -1.742786 -1.493817 -1.244847 -0.995878 -0.746908 -0.497939 -0.248969 2019-04-25 00:42:16
14 3.030929 2.755390 2.479851 2.204312 1.928773 1.653234 1.377695 1.102156 0.826617 0.551078 ... -2.479851 -2.204312 -1.928773 -1.653234 -1.377695 -1.102156 -0.826617 -0.551078 -0.275539 2019-04-27 12:42:16
15 3.323945 3.021768 2.719591 2.417414 2.115238 1.813061 1.510884 1.208707 0.906530 0.604354 ... -2.719591 -2.417414 -2.115238 -1.813061 -1.510884 -1.208707 -0.906530 -0.604354 -0.302177 2019-04-27 18:42:16
16 3.096985 2.815441 2.533897 2.252353 1.970808 1.689264 1.407720 1.126176 0.844632 0.563088 ... -2.533897 -2.252353 -1.970808 -1.689264 -1.407720 -1.126176 -0.844632 -0.563088 -0.281544 2019-04-28 00:42:16
17 3.014419 2.740381 2.466343 2.192305 1.918267 1.644229 1.370190 1.096152 0.822114 0.548076 ... -2.466343 -2.192305 -1.918267 -1.644229 -1.370190 -1.096152 -0.822114 -0.548076 -0.274038 2019-04-28 12:42:16
18 2.997955 2.725413 2.452872 2.180331 1.907789 1.635248 1.362707 1.090165 0.817624 0.545083 ... -2.452872 -2.180331 -1.907789 -1.635248 -1.362707 -1.090165 -0.817624 -0.545083 -0.272541 2019-04-28 18:42:16
19 3.054675 2.776977 2.499280 2.221582 1.943884 1.666186 1.388489 1.110791 0.833093 0.555395 ... -2.499280 -2.221582 -1.943884 -1.666186 -1.388489 -1.110791 -0.833093 -0.555395 -0.277698 2019-04-29 12:42:16
20 2.940178 2.672889 2.405600 2.138311 1.871022 1.603733 1.336444 1.069155 0.801867 0.534578 ... -2.405600 -2.138311 -1.871022 -1.603733 -1.336444 -1.069155 -0.801867 -0.534578 -0.267289 2019-04-29 18:42:16
21 2.822523 2.565930 2.309337 2.052744 1.796151 1.539558 1.282965 1.026372 0.769779 0.513186 ... -2.309337 -2.052744 -1.796151 -1.539558 -1.282965 -1.026372 -0.769779 -0.513186 -0.256593 2019-04-30 00:42:16
22 2.781395 2.528540 2.275686 2.022832 1.769978 1.517124 1.264270 1.011416 0.758562 0.505708 ... -2.275686 -2.022832 -1.769978 -1.517124 -1.264270 -1.011416 -0.758562 -0.505708 -0.252854 2019-04-30 06:42:16
23 3.369302 3.063002 2.756701 2.450401 2.144101 1.837801 1.531501 1.225201 0.918900 0.612600 ... -2.756701 -2.450401 -2.144101 -1.837801 -1.531501 -1.225201 -0.918900 -0.612600 -0.306300 2019-04-30 12:42:16
24 3.402459 3.093144 2.783830 2.474515 2.165201 1.855887 1.546572 1.237258 0.927943 0.618629 ... -2.783830 -2.474515 -2.165201 -1.855887 -1.546572 -1.237258 -0.927943 -0.618629 -0.309314 2019-04-30 18:42:16
25 3.501498 3.183180 2.864862 2.546544 2.228226 1.909908 1.591590 1.273272 0.954954 0.636636 ... -2.864862 -2.546544 -2.228226 -1.909908 -1.591590 -1.273272 -0.954954 -0.636636 -0.318318 2019-05-01 12:42:16
26 3.284622 2.986020 2.687418 2.388816 2.090214 1.791612 1.493010 1.194408 0.895806 0.597204 ... -2.687418 -2.388816 -2.090214 -1.791612 -1.493010 -1.194408 -0.895806 -0.597204 -0.298602 2019-05-01 18:42:16
27 3.232952 2.939048 2.645143 2.351238 2.057333 1.763429 1.469524 1.175619 0.881714 0.587810 ... -2.645143 -2.351238 -2.057333 -1.763429 -1.469524 -1.175619 -0.881714 -0.587810 -0.293905 2019-05-02 00:42:16
28 3.337506 3.034096 2.730686 2.427277 2.123867 1.820458 1.517048 1.213638 0.910229 0.606819 ... -2.730686 -2.427277 -2.123867 -1.820458 -1.517048 -1.213638 -0.910229 -0.606819 -0.303410 2019-05-02 06:42:16
29 4.160436 3.782214 3.403993 3.025772 2.647550 2.269329 1.891107 1.512886 1.134664 0.756443 ... -3.403993 -3.025772 -2.647550 -2.269329 -1.891107 -1.512886 -1.134664 -0.756443 -0.378221 2019-05-02 12:42:16
30 4.605202 4.186547 3.767892 3.349238 2.930583 2.511928 2.093274 1.674619 1.255964 0.837309 ... -3.767892 -3.349238 -2.930583 -2.511928 -2.093274 -1.674619 -1.255964 -0.837309 -0.418655 2019-05-03 18:42:16
31 4.985494 4.532267 4.079040 3.625814 3.172587 2.719360 2.266134 1.812907 1.359680 0.906453 ... -4.079040 -3.625814 -3.172587 -2.719360 -2.266134 -1.812907 -1.359680 -0.906453 -0.453227 2019-05-04 06:42:16
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
476 -1.742141 -1.583765 -1.425388 -1.267012 -1.108635 -0.950259 -0.791882 -0.633506 -0.475129 -0.316753 ... 1.425388 1.267012 1.108635 0.950259 0.791882 0.633506 0.475129 0.316753 0.158376 2019-08-24 18:42:16
477 -1.423766 -1.294333 -1.164900 -1.035466 -0.906033 -0.776600 -0.647166 -0.517733 -0.388300 -0.258867 ... 1.164900 1.035466 0.906033 0.776600 0.647166 0.517733 0.388300 0.258867 0.129433 2019-08-25 00:42:16
478 -1.364295 -1.240268 -1.116241 -0.992215 -0.868188 -0.744161 -0.620134 -0.496107 -0.372080 -0.248054 ... 1.116241 0.992215 0.868188 0.744161 0.620134 0.496107 0.372080 0.248054 0.124027 2019-08-25 06:42:16
479 -1.502851 -1.366228 -1.229605 -1.092983 -0.956360 -0.819737 -0.683114 -0.546491 -0.409868 -0.273246 ... 1.229605 1.092983 0.956360 0.819737 0.683114 0.546491 0.409868 0.273246 0.136623 2019-08-25 12:42:16
480 -1.772703 -1.611548 -1.450393 -1.289238 -1.128084 -0.966929 -0.805774 -0.644619 -0.483464 -0.322310 ... 1.450393 1.289238 1.128084 0.966929 0.805774 0.644619 0.483464 0.322310 0.161155 2019-08-25 18:42:16
481 -1.849329 -1.681209 -1.513088 -1.344967 -1.176846 -1.008725 -0.840604 -0.672483 -0.504363 -0.336242 ... 1.513088 1.344967 1.176846 1.008725 0.840604 0.672483 0.504363 0.336242 0.168121 2019-08-26 00:42:16
482 -1.785413 -1.623103 -1.460792 -1.298482 -1.136172 -0.973862 -0.811551 -0.649241 -0.486931 -0.324621 ... 1.460792 1.298482 1.136172 0.973862 0.811551 0.649241 0.486931 0.324621 0.162310 2019-08-26 06:42:16
483 -1.719841 -1.563492 -1.407143 -1.250794 -1.094445 -0.938095 -0.781746 -0.625397 -0.469048 -0.312698 ... 1.407143 1.250794 1.094445 0.938095 0.781746 0.625397 0.469048 0.312698 0.156349 2019-08-26 12:42:16
484 -1.686237 -1.532943 -1.379648 -1.226354 -1.073060 -0.919766 -0.766471 -0.613177 -0.459883 -0.306589 ... 1.379648 1.226354 1.073060 0.919766 0.766471 0.613177 0.459883 0.306589 0.153294 2019-08-26 18:42:16
485 -1.690967 -1.537243 -1.383518 -1.229794 -1.076070 -0.922346 -0.768621 -0.614897 -0.461173 -0.307449 ... 1.383518 1.229794 1.076070 0.922346 0.768621 0.614897 0.461173 0.307449 0.153724 2019-08-27 00:42:16
486 -1.658665 -1.507877 -1.357090 -1.206302 -1.055514 -0.904726 -0.753939 -0.603151 -0.452363 -0.301575 ... 1.357090 1.206302 1.055514 0.904726 0.753939 0.603151 0.452363 0.301575 0.150788 2019-08-27 06:42:16
487 -1.624350 -1.476682 -1.329014 -1.181346 -1.033677 -0.886009 -0.738341 -0.590673 -0.443005 -0.295336 ... 1.329014 1.181346 1.033677 0.886009 0.738341 0.590673 0.443005 0.295336 0.147668 2019-08-27 12:42:16
488 -1.638594 -1.489631 -1.340668 -1.191705 -1.042742 -0.893779 -0.744816 -0.595852 -0.446889 -0.297926 ... 1.340668 1.191705 1.042742 0.893779 0.744816 0.595852 0.446889 0.297926 0.148963 2019-08-27 18:42:16
489 -1.666868 -1.515334 -1.363801 -1.212268 -1.060734 -0.909201 -0.757667 -0.606134 -0.454600 -0.303067 ... 1.363801 1.212268 1.060734 0.909201 0.757667 0.606134 0.454600 0.303067 0.151533 2019-08-28 00:42:16
490 -1.666989 -1.515445 -1.363900 -1.212356 -1.060811 -0.909267 -0.757722 -0.606178 -0.454633 -0.303089 ... 1.363900 1.212356 1.060811 0.909267 0.757722 0.606178 0.454633 0.303089 0.151544 2019-08-28 06:42:16
491 -1.657216 -1.506560 -1.355904 -1.205248 -1.054592 -0.903936 -0.753280 -0.602624 -0.451968 -0.301312 ... 1.355904 1.205248 1.054592 0.903936 0.753280 0.602624 0.451968 0.301312 0.150656 2019-08-28 12:42:16
492 -1.660107 -1.509188 -1.358269 -1.207350 -1.056431 -0.905513 -0.754594 -0.603675 -0.452756 -0.301838 ... 1.358269 1.207350 1.056431 0.905513 0.754594 0.603675 0.452756 0.301838 0.150919 2019-08-28 18:42:16
493 -1.677191 -1.524719 -1.372247 -1.219775 -1.067303 -0.914831 -0.762360 -0.609888 -0.457416 -0.304944 ... 1.372247 1.219775 1.067303 0.914831 0.762360 0.609888 0.457416 0.304944 0.152472 2019-08-29 00:42:16
494 -1.660338 -1.509398 -1.358458 -1.207518 -1.056578 -0.905639 -0.754699 -0.603759 -0.452819 -0.301880 ... 1.358458 1.207518 1.056578 0.905639 0.754699 0.603759 0.452819 0.301880 0.150940 2019-08-29 06:42:16
495 -1.654214 -1.503831 -1.353448 -1.203065 -1.052681 -0.902298 -0.751915 -0.601532 -0.451149 -0.300766 ... 1.353448 1.203065 1.052681 0.902298 0.751915 0.601532 0.451149 0.300766 0.150383 2019-08-29 12:42:16
496 -1.628582 -1.480529 -1.332476 -1.184423 -1.036370 -0.888317 -0.740264 -0.592211 -0.444159 -0.296106 ... 1.332476 1.184423 1.036370 0.888317 0.740264 0.592211 0.444159 0.296106 0.148053 2019-08-29 18:42:16
497 -1.606709 -1.460645 -1.314580 -1.168516 -1.022451 -0.876387 -0.730322 -0.584258 -0.438193 -0.292129 ... 1.314580 1.168516 1.022451 0.876387 0.730322 0.584258 0.438193 0.292129 0.146064 2019-08-30 00:42:16
498 -1.603597 -1.457815 -1.312034 -1.166252 -1.020471 -0.874689 -0.728908 -0.583126 -0.437345 -0.291563 ... 1.312034 1.166252 1.020471 0.874689 0.728908 0.583126 0.437345 0.291563 0.145782 2019-08-30 06:42:16
499 -1.617874 -1.470795 -1.323715 -1.176636 -1.029556 -0.882477 -0.735397 -0.588318 -0.441238 -0.294159 ... 1.323715 1.176636 1.029556 0.882477 0.735397 0.588318 0.441238 0.294159 0.147079 2019-08-30 12:42:16
500 -1.637517 -1.488652 -1.339787 -1.190921 -1.042056 -0.893191 -0.744326 -0.595461 -0.446596 -0.297730 ... 1.339787 1.190921 1.042056 0.893191 0.744326 0.595461 0.446596 0.297730 0.148865 2019-08-30 18:42:16
501 -1.635322 -1.486656 -1.337991 -1.189325 -1.040659 -0.891994 -0.743328 -0.594663 -0.445997 -0.297331 ... 1.337991 1.189325 1.040659 0.891994 0.743328 0.594663 0.445997 0.297331 0.148666 2019-08-31 00:42:16
502 -1.663925 -1.512659 -1.361393 -1.210127 -1.058861 -0.907595 -0.756329 -0.605064 -0.453798 -0.302532 ... 1.361393 1.210127 1.058861 0.907595 0.756329 0.605064 0.453798 0.302532 0.151266 2019-08-31 06:42:16
503 -1.635475 -1.486796 -1.338116 -1.189436 -1.040757 -0.892077 -0.743398 -0.594718 -0.446039 -0.297359 ... 1.338116 1.189436 1.040757 0.892077 0.743398 0.594718 0.446039 0.297359 0.148680 2019-08-31 12:42:16
504 -1.671526 -1.519569 -1.367612 -1.215655 -1.063698 -0.911741 -0.759785 -0.607828 -0.455871 -0.303914 ... 1.367612 1.215655 1.063698 0.911741 0.759785 0.607828 0.455871 0.303914 0.151957 NaT
505 -1.631945 -1.483586 -1.335227 -1.186869 -1.038510 -0.890152 -0.741793 -0.593434 -0.445076 -0.296717 ... 1.335227 1.186869 1.038510 0.890152 0.741793 0.593434 0.445076 0.296717 0.148359 NaT

504 rows × 23 columns

In [12]:
data = [go.Scatter(
        x=backtest_transpose['start_time'],
        y=backtest_transpose[c]
    ) for c in backtest_transpose.columns if c != 'start_time']

layout = go.Layout(
    title='Profits compared to direct investment in cDAI',
    yaxis=dict(title='Profits'),
    xaxis=dict(title='Date'),
    template='plotly_white')

figure = go.Figure(data=data, layout=layout)

figure.show()

Long Positions only

Next, extract only the long positions and plot their value increase over time.

In [13]:
backtest_transpose_long = backtest[backtest.is_long == 1].iloc[:,7:].transpose()
backtest_transpose_long['start_time'] = interestOverTime.iloc[:len(backtest_transpose_long.iloc[:, 0]), 3]
In [14]:
data = [go.Scatter(
        x=backtest_transpose_long['start_time'],
        y=backtest_transpose_long[c]
    ) for c in backtest_transpose_long.columns if c != 'start_time']

layout = go.Layout(
    title='Profits compared to direct investment in cDAI - only long positions',
    yaxis=dict(title='Profits'),
    xaxis=dict(title='Date'),
    template='plotly_white')

figure = go.Figure(data=data, layout=layout)

figure.show()
In [ ]: